//package com.bite.filter;
//
//import jakarta.servlet.*;
//import jakarta.servlet.http.HttpServletRequest;
//import jakarta.servlet.http.HttpServletRequestWrapper;
//import org.springframework.stereotype.Component;
//import org.springframework.util.StreamUtils;
//
//import java.io.BufferedReader;
//import java.io.ByteArrayInputStream;
//import java.io.IOException;
//import java.io.InputStreamReader;
//import java.nio.charset.StandardCharsets;
//
//@Component
//public class WebHookFilter implements Filter {
//    @Override
//    public void init(FilterConfig filterConfig) throws ServletException {
//    }
//
//    @Override
//    public void destroy() {
//    }
//
//    @Override
//    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
//        String url = new String(httpServletRequest.getRequestURI());
//
//        //检查url是否以/refresh结尾, 如果不是, 直接把请求传递给下一个过滤器或者目标资源
//        if (!url.endsWith("/refresh")) {
//            filterChain.doFilter(servletRequest, servletResponse);
//            return;
//        }
//
//        // 对于 /refresh 端点，保留原始请求体以便控制器可以读取密码
//        // 如果请求体为空或者是 GET 请求，则传递空请求体（兼容 Actuator 的 refresh 端点）
//        RequestWrapper requestWrapper = new RequestWrapper(httpServletRequest);
//        filterChain.doFilter(requestWrapper, servletResponse);
//    }
//
//    private class RequestWrapper extends HttpServletRequestWrapper {
//        private byte[] cachedBody;
//
//        public RequestWrapper(HttpServletRequest request) {
//            super(request);
//            try {
//                // 读取并缓存请求体
//                cachedBody = StreamUtils.copyToByteArray(request.getInputStream());
//            } catch (IOException e) {
//                cachedBody = new byte[0];
//            }
//        }
//
//        @Override
//        public ServletInputStream getInputStream() throws IOException {
//            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(cachedBody);
//            return new ServletInputStream() {
//                @Override
//                public int read() throws IOException {
//                    return byteArrayInputStream.read();
//                }
//
//                @Override
//                public boolean isFinished() {
//                    return byteArrayInputStream.available() == 0;
//                }
//
//                @Override
//                public boolean isReady() {
//                    return true;
//                }
//
//                @Override
//                public void setReadListener(ReadListener listener) {
//                    // 不需要实现
//                }
//            };
//        }
//
//        @Override
//        public BufferedReader getReader() throws IOException {
//            return new BufferedReader(new InputStreamReader(getInputStream(), StandardCharsets.UTF_8));
//        }
//    }
//}
